package com.example.androidproject.ui.fragment;

import android.app.Activity;
import android.view.View;
import android.widget.Button;

import com.example.androidproject.R;
import com.example.androidproject.net.RetrofitHelper;
import com.example.androidproject.ui.base.IBaseFragment;
import com.hyg.base.ui.base.BasePresenter;
import com.hyg.base.ui.common.FragmentContainerActivity;
import com.hyg.common.utils.ToastUtils;
import com.hyg.idea.net.common.APICallback;

import autodispose2.AutoDispose;
import autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider;
import butterknife.BindView;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * FileName: TestMvcFragment
 * Author: huangyuguang
 * Date: 2022/5/11
 * Description: mvc
 */
public class TestMvcFragment extends IBaseFragment {
    @BindView(R.id.btn)
    Button button;

    private CompositeDisposable compositeDisposable = new CompositeDisposable();

    public static void launch(Activity from){
        FragmentContainerActivity.getInstance().launch(from, TestMvcFragment.class);
    }

    @Override
    public String initTitle() {
        return "试试看MVC";
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public int setNewFragmentContainerView() {
        return R.layout.comm_fragment_container1;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initViewAndData() {
        button.setText("hahah");
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.show("哈哈哈");
            }
        });
    }

    private void getTestData(){
        RetrofitHelper.getApiService().getTest()
                .compose(rxSchedulerHelper(true))
                .subscribe(new APICallback<String>() {
                    @Override
                    public void onSuccess(String response) {
                    }

                    @Override
                    public void onFail(String message) {
                        super.onFail(message);
                    }
                });
    }

    /**
     * AutoDispose的使用
     */
    private void autoDisposable(){
        Disposable disposable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                //do something
                if (!emitter.isDisposed()){
                    emitter.onNext("");
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .to(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String data) throws Exception {
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {
                        throwable.printStackTrace();
                        showToast(throwable.getMessage());
                    }
                });
        compositeDisposable.add(disposable);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        compositeDisposable.clear();
    }
}
